<template>
    <!-- 添加 -->
    <div class="add-box">
        <div class="data-list">
            <el-icon>
                <Tickets />
            </el-icon>
            <span>数据列表</span>
        </div>
        <el-button type="default" @click="router.push('/pms/addProductCate')">添加</el-button>
    </div>
    <!-- table数据 -->
    <el-table :data="tableData" border fit>
        <el-table-column prop="id" label="编号"></el-table-column>
        <el-table-column prop="name" label="分类名称"></el-table-column>
        <el-table-column prop="level" label="级别">
            <template #default="scope">
                <span>{{ scope.row.level === 0 ? '一级' : '二级' }}</span>
            </template>
        </el-table-column>
        <el-table-column prop="productCount" label="商品数量"></el-table-column>
        <el-table-column prop="productUnit" label="数量单位"></el-table-column>
        <el-table-column prop="navStatus" label="导航栏">
            <template #default="scope">
                <el-switch v-model="scope.row.navStatus" :active-value="1" :inactive-value="0"
                    @change="changeNavStatusFn(scope.row)">
                </el-switch>
            </template>
        </el-table-column>
        <el-table-column prop="showStatus" label="是否显示">
            <template #default="scope">
                <el-switch v-model="scope.row.showStatus" :active-value="1" :inactive-value="0"
                    @change="changeShowStatusFn(scope.row)">
                </el-switch>
            </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序"></el-table-column>
        <el-table-column label="设置" width="200">
            <template #default="scope" class="make">
                <el-button type="default" size="small" @click="checkChildFn(scope.row.id)"
                    :disabled="scope.row.level === 1">查看下级</el-button>
                <el-button type="default" size="small">转移商品</el-button>
            </template>
        </el-table-column>
        <el-table-column label="操作" width="160">
            <template #default="scope">
                <el-button type="default" size="small" @click="editFn(scope.row.id)">编辑</el-button>
                <el-button type="danger" size="small" @click="delFn(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <!-- 分页 -->
    <ElConfigProvider :locale="zhCn">
        <el-pagination v-model:currentPage="pageNum" v-model:page-size="pageSize" background
            layout="total, sizes, prev, pager, next ,jumper" :total="total" @size-change="pageSizeChange"
            @current-change="pageNumChange" :page-sizes="[5, 10, 15]" />
    </ElConfigProvider>
</template>
<script setup lang='ts'>
import { Search, Tickets } from '@element-plus/icons-vue';
import { useTable } from '@/hooks/useTable'
import { pro } from '@/api'
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { ElConfigProvider, ElMessage, ElMessageBox } from "element-plus";
import { reactive } from 'vue';
import { useRouter, useRoute } from 'vue-router';
const emit = defineEmits(['updateKey'])
const route = useRoute()
const router = useRouter()
const params = reactive({
    parentId: route.query.parentId ? route.query.parentId : 0,
    pageSize: 5
})
// 请求
const { tableData, pageNum, pageSize, total, pageNumChange, pageSizeChange, search } = useTable(pro.queryProductCategoryListApi, params);
// pageSize.value = 5

// 编辑
const editFn = (id: number) => {
    router.push(`/pms/addProductCate?id=${id}`)
}
// 查看下级
const checkChildFn = (parentId: number) => {
    params.parentId = parentId;
    router.push(`/pms/productCate?parentId=${parentId}`)
}
//删除
const delFn = (id: number) => {
    ElMessageBox.confirm('确定要删除该数据吗？', '提示').then(() => {
        pro.delIDCategoryApi(id).then(res => {
            ElMessage({
                message: res.code === 200 ? '删除成功' : res.message,
                type: res.code === 200 ? 'success' : 'error'
            })
            // tableData.value = useTable(pro.queryProductCategoryListApi, params).tableData;
            if (res.code === 200) {
                search()
                // useTable(pro.queryProductCategoryListApi, params)
            }
        })
    })
}
// 修改导航状态uptProCatShowStatusApi
const changeNavStatusFn = (row: IProductCategoryModel) => {
    console.log('row', row);
    pro.uptProCateNavStatusApi({ ids: [row.id], navStatus: row.navStatus }).then(res => {
        if (res.code === 200) {
            ElMessage.success('修改成功')
        }
    }).catch(err => {
        row.navStatus === 1 ? 0 : 1
    })
}
// 是否显示
const changeShowStatusFn = (row: IProductCategoryModel) => {
    console.log('row', row);
    pro.uptProCatShowStatusApi({ ids: [row.id], showStatus: row.showStatus }).then(res => {
        if (res.code === 200) {
            ElMessage.success('修改成功')
        }
    }).catch(err => {
        row.showStatus === 1 ? 0 : 1
    })
}
</script>
<style lang="less" scoped>
.add-box {
    padding: 15px;
    display: flex;
    justify-content: space-between;
    border: 1px solid #ebeef5;
    border-radius: 5px;
    align-items: center;
    margin-bottom: 20px;

    .data-list {
        display: flex;
        align-items: center;
    }
}

.el-pagination {
    margin-top: 20px;
    justify-content: end;
}
</style>